package com.hancher.common.javautil;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * 作者 : Hancher ytu_shaoweijie@163.com <br/>
 * 时间 : 2023/8/1 13:56 <br/>
 * 描述 :
 */
public class Md5Util {


    /**
     * md5 加密
     *
     * @param string 输入
     * @return 输出
     */
    public static String getStrMd5(String string) throws NoSuchAlgorithmException, IllegalArgumentException {
        if (EmptyUtil.isEmpty(string)) {
            throw new IllegalArgumentException("入参为空");
        }
        MessageDigest md5 = MessageDigest.getInstance("MD5");
        byte[] bytes = md5.digest(string.getBytes(StandardCharsets.UTF_8));
        return ByteStrUtil.byte2Str(bytes);
    }

    /**
     * 加盐Md5 加密
     *
     * @param string 输入
     * @param slat   盐
     * @return 输出
     */
    public static String getStrMd5OnSlat(String string, String slat) throws NoSuchAlgorithmException, IllegalArgumentException {
        if (EmptyUtil.isEmpty(string) || EmptyUtil.isEmpty(slat)) {
            throw new IllegalArgumentException("入参为空");
        }
        MessageDigest md5 = MessageDigest.getInstance("MD5");
        byte[] bytes = md5.digest((string + slat).getBytes(StandardCharsets.UTF_8));
        return ByteStrUtil.byte2Str(bytes);
    }

    /**
     * 多次md5 加密
     *
     * @param string 字符串
     * @param times  总次数
     * @return 加密的字符串
     */
    public static String getStrMd5ByRepeated(String string, int times) throws NoSuchAlgorithmException, IllegalArgumentException {
        if (EmptyUtil.isEmpty(string)) {
            throw new IllegalArgumentException("入参为空");
        }
        String md5 = getStrMd5(string);
        for (int i = 0; i < times - 1; i++) {
            md5 = getStrMd5(md5);
        }
        return md5;
    }

    /**
     * 文件md5 加密
     *
     * @param file 输入文件
     * @return 输出
     */
    public static String getFileMd5(File file) throws NoSuchAlgorithmException, IllegalArgumentException {
        if (file == null || !file.isFile() || !file.exists()) {
            throw new IllegalArgumentException("文件不存在");
        }
        FileInputStream in = null;
        String result = "";
        byte buffer[] = new byte[8192];
        int len;
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            in = new FileInputStream(file);
            while ((len = in.read(buffer)) != -1) {
                md5.update(buffer, 0, len);
            }
            byte[] bytes = md5.digest();

            for (byte b : bytes) {
                String temp = Integer.toHexString(b & 0xff);
                if (temp.length() == 1) {
                    temp = "0" + temp;
                }
                result += temp;
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != in) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }
}
